package middlewares

import (
	"fmt"
	"log"
	"net/http"
	"path"
)

// HttpLog 请求日志
func RequestRouteLog(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		passPrefix := []string{"jpg", "png", "jpeg", "css", "js", "woff", "woff2"}
		urlPath := r.URL.Path
		ext := path.Ext(urlPath)
		temp := "[" + r.Method + "]:" + urlPath
		inLog := true //是否将静态文件输出到日志
		for _, v := range passPrefix {
			tExt := "." + v
			if tExt == ext {
				inLog = false
			}
		}
		if inLog {
			err := log.Output(1, temp)
			if err != nil {
				fmt.Println(err.Error())
			}
		}
		// 2. 将请求传递下去
		next.ServeHTTP(w, r)
	})
}
